﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
using KOPLibrary.DataManagement;


namespace KOPLibrary
{
    public class WordManager
    {
        static object vk_read_only = false;
        static object vk_visible = true;
        static object vk_false = false;
        public static object vk_true = true;
        static object vk_dynamic = 2;
        static object vk_missing = Type.Missing;

        private Microsoft.Office.Interop.Word.Application app;

        public WordManager(Microsoft.Office.Interop.Word.Application app)
        {
            this.app = app;
        }


        public Microsoft.Office.Interop.Word.Document CreateWordDocument()
        {
            return CreateWordDocument(string.Empty);
        }


        public Microsoft.Office.Interop.Word.Document CreateWordDocument(bool visible)
        {
            if (!visible)
            {
                var newDoc = app.Documents.Add(ref vk_missing, ref vk_false, ref vk_missing, ref vk_false);
                return newDoc;
            }
            else
            {
                var newDoc = app.Documents.Add(ref vk_missing, ref vk_false, ref vk_missing, ref vk_true);
                return newDoc;
            }

        }


        public Microsoft.Office.Interop.Word.Document CreateWordDocument(string baseTemplate)
        {
            object templatePath = baseTemplate;
            if (string.IsNullOrEmpty(baseTemplate))
            {

                var newDoc = app.Documents.Add(ref vk_missing, ref vk_false, ref vk_missing, ref vk_true);

                return newDoc;
            }
            else
            {

                var newDoc = app.Documents.Add(ref templatePath, ref vk_false, ref vk_missing, ref vk_true);

                return newDoc;
            }
        }

        public Microsoft.Office.Interop.Word.Document OpenWordDocument(string document, string pwd)
        {
            object doc = document;
            object docPwd = pwd;
            var wordDoc = app.Documents.Open(ref doc, PasswordDocument: docPwd, AddToRecentFiles: false);
            return wordDoc;
        }

        public void BindDocument(Microsoft.Office.Interop.Word.Document doc, AbstractData data, int current, List<ContentControl> controls)
        {
            int index; object tempobject;
            
            foreach (Microsoft.Office.Interop.Word.ContentControl item in controls)
            {                
                index = data.Columns.IndexOf(item.Title);
                if (index == -1) continue;
                tempobject = data.ItemArray[current][index];
                try
                {
                    item.Range.Text = (tempobject != null) ? tempobject.ToString() : string.Empty;
                }
                catch { continue; }
            }
        }

        public void BindDocument(Microsoft.Office.Interop.Word.Document doc, AbstractData data, int current)
        {
            BindDocument(doc, data, current, GetContentControlsFromDoc(doc, true));

        }

        internal Microsoft.Office.Interop.Word.Document CreateMailingDocument(bool visible)
        {
            var doc = CreateWordDocument(visible);
            doc.Application.ShowMe();
            doc.Activate();
            app.ActiveDocument.MailMerge.MainDocumentType = WdMailMergeMainDocType.wdMailingLabels;                     
            int result = app.Dialogs[WdWordDialog.wdDialogLabelOptions].Show();            
            return app.ActiveDocument;
        }

        public List<ContentControl> GetContentControlsFromDoc(Document doc, bool OnlyTextTypeControl)
        {
            List<ContentControl> controls = new List<ContentControl>();
            foreach (Microsoft.Office.Interop.Word.ContentControl item in doc.ContentControls)
            {
                if (item.Type == WdContentControlType.wdContentControlBuildingBlockGallery ||
                    item.Type == WdContentControlType.wdContentControlGroup) continue;
                if (OnlyTextTypeControl)
                {
                    if (item.Type == WdContentControlType.wdContentControlPicture) continue;
                }
                controls.Add(item);
            }

            foreach (Shape item in doc.Shapes)
            {
                if (item.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox || item.Type == Microsoft.Office.Core.MsoShapeType.msoAutoShape)
                {
                    item.Select();
                    foreach (ContentControl inneritem in doc.Application.Selection.Range.ContentControls)
                    {
                        if (inneritem.Type == WdContentControlType.wdContentControlBuildingBlockGallery ||
                     inneritem.Type == WdContentControlType.wdContentControlGroup ) continue;
                        if (OnlyTextTypeControl)
                        {
                            if (inneritem.Type == WdContentControlType.wdContentControlPicture) continue;
                        }
                        controls.Add(inneritem);
                    }
                }
            }
            doc.Application.Selection.EndKey(WdUnits.wdStory, WdMovementType.wdMove);
            return controls;
        }
    }
}
